package com.jt.assistant.ui

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.afollestad.materialdialogs.list.listItems
import com.blankj.utilcode.util.ToastUtils
import com.jt.assistant.R
import com.jt.assistant.adapter.CarSearchAdapter
import com.jt.assistant.bean.Car
import com.jt.assistant.bean.Param
import com.jt.assistant.viewmodel.HomeViewModel
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_car_search.*

/**
 * 车辆查询
 */
class CarSearchActivity : BaseActivity<HomeViewModel>() {

    private val mAdapter by lazy {
        CarSearchAdapter(
            ArrayList<Car>()
        )
    }
    private var mType = "1"
    private var mDay = "1"

    val mSearchList = ArrayList<Car>()

    override fun layoutId(): Int = R.layout.activity_car_search

    override fun initView(savedInstanceState: Bundle?) {
        with(rv_carSearch) {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
    }

    override fun initData() {
        val params: Param = Param::class.java.newInstance()
        params.type = mType
        params.day = mDay
        viewModel.getCarList(params)
    }

    override fun initListener() {
        tv_car_direction.setOnClickListener {
            showDirectionListDialog()
        }

        tv_car_time.setOnClickListener {
            showTimeListDialog()
        }

        set_carSearch.setOnScanListener {
            if (it.isNullOrEmpty()) {
                val params: Param = Param::class.java.newInstance()
                params.type = mType
                params.day = mDay
                viewModel.getCarList(params)
            } else {
                mAdapter.data.clear()
                mAdapter.data.addAll(findCar(it))
                mAdapter.notifyDataSetChanged()
                if (mAdapter.data.isEmpty())ToastUtils.showShort("未搜索到数据")
            }
        }

        viewModel.mGetCarList.observe(this, {
            if (it.isSuccess()) {
                if (it.data.carList.isEmpty()) ToastUtils.showShort("没有数据")
                mAdapter.data.clear()
                mSearchList.clear()
                mAdapter.data.addAll(it.data.carList)
                mSearchList.addAll(it.data.carList)
                mAdapter.notifyDataSetChanged()
            }
        })

        mAdapter.setOnItemClickListener { adapter, view, position ->
            startActivity(Intent(this@CarSearchActivity, CarCostActivity::class.java).apply {
                putExtra("Id", mAdapter.data[position].id)
                putExtra("Type", mType)
            })
        }
    }


    private fun findCar(s: String): ArrayList<Car> {
        val dataList = ArrayList<Car>()
        for (i in mSearchList.indices) {
            if (mSearchList[i].license.contains(s)) {
                dataList.add(mSearchList[i])
            }
        }
        return dataList
    }

    private fun showDirectionListDialog() {
        val list = listOf(
            "送货车辆",
            "退货车辆"
        )

        MaterialDialog(this).show {
            listItems(items = list) { _, index, text ->
                this@CarSearchActivity.tv_car_direction.text = text
                mType = (index + 1).toString()//1 送货  2退货

                val params = Param::class.java.newInstance()
                params.type = mType
                params.day = mDay
                viewModel.getCarList(params)
            }
            lifecycleOwner(this@CarSearchActivity)
        }
    }


    private fun showTimeListDialog() {
        val list = listOf(
            "近一天",
            "近三天",
            "近七天",
        )

        MaterialDialog(this).show {
            listItems(items = list) { _, index, text ->
                this@CarSearchActivity.tv_car_time.text = text
                mDay = when (index) {
                    0 -> {
                        "1"
                    }
                    1 -> {
                        "3"
                    }
                    else -> {
                        "7"
                    }
                }

                val params = Param::class.java.newInstance()
                params.type = mType
                params.day = mDay
                viewModel.getCarList(params)
            }
            lifecycleOwner(this@CarSearchActivity)
        }
    }


}